package com.jp.juc.tools;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.Exchanger;

public class UseExchange {

    private static final Exchanger<Set<String>>
            EXCHANGER = new Exchanger<>();

    public static void main(String[] args) {
        new Thread(() -> {
            // //存放数据的容器
            Set<String> setA = new HashSet<>();
            setA.add("A");
            try {
                setA = EXCHANGER.exchange(setA);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("SET A" + setA);
        }).start();

        new Thread(() -> {
            Set<String> setB = new HashSet<>();
            setB.add("B");
            try {
                setB = EXCHANGER.exchange(setB);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("SET B" + setB);
        }
        ).start();
    }
}
